package com.zbkj.crmeb.merchant.admin.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.merchant.admin.service.CustomShareService;
import com.zbkj.crmeb.merchant.api.response.MerchantOrderTimeResponse;
import com.zbkj.crmeb.store.model.CustomShare;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户 -- 用户中心
 */
@Slf4j
@RestController("CustomShareController")
@RequestMapping("api/admin/mer/store/share")
@Api(tags = "分享配置")
public class MerCustomShareController extends AdminBaseController{

    @Autowired
    CustomShareService customShareService;


    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<CustomShare> info(@RequestParam(value = "merId") Integer merId){
        CustomShare customShare = customShareService.getOne(Wrappers.<CustomShare>lambdaQuery().eq(CustomShare::getMerId,merId).last("LIMIT 1"));
        return CommonResult.success(customShare);
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public CommonResult<Object> updateCustomShare(@Validated CustomShare customShare, HttpServletRequest servletRequest){
        //customShare.setMerId(getMerId(servletRequest));
        return CommonResult.success(customShareService.saveOrUpdate(customShare));
    }

    @ApiOperation(value = "订单时效查询")
    @RequestMapping(value = "/getOrderTime", method = RequestMethod.GET)
    public CommonResult<MerchantOrderTimeResponse> getOrderTime(HttpServletRequest servletRequest){
        return CommonResult.success(customShareService.getOrderTime(getMerId(servletRequest)));
    }

    @ApiOperation(value = "订单时效修改")
    @RequestMapping(value = "/updOrderTime", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderTime", value="普通订单时效"),
            @ApiImplicitParam(name="activityOrderTime", value="活动订单时效")
    })
    public CommonResult<Object> updOrderTime(@RequestParam Integer orderTime, @RequestParam Integer activityOrderTime, HttpServletRequest servletRequest){
        if (orderTime < 1 || orderTime > 999999){
            return CommonResult.failed("普通订单时效需大于0且小于1000000");
        }
        if (activityOrderTime < 1 || activityOrderTime > 999999){
            return CommonResult.failed("活动订单时效需大于0且小于1000000");
        }

        return customShareService.updOrderTime(orderTime, activityOrderTime ,getMerId(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

}



